package com.team7.happycommunity.businessservice.controller;

import com.team7.happycommunity.businessservice.common.CommonResult;
import com.team7.happycommunity.businessservice.entity.BusinessImage;
import com.team7.happycommunity.businessservice.service.BusinessImageService;
import com.team7.happycommunity.businessservice.util.QiniuUploadUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 * (BusinessImage)表控制层
 *
 * @author makejava
 * @since 2020-03-25 20:07:28
 */
@RestController
@RequestMapping("businessImage")
@Api(tags="商家图片管理接口")
public class BusinessImageController {
    /**
     * 服务对象
     */
    @Resource
    private BusinessImageService businessImageService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public BusinessImage selectOne(Integer id) {
        return this.businessImageService.queryById(id);
    }


    /**
     * 图片上传或者修改
     * @param file 图片
     * @param userId 用户id
     * @return
     */
    @PostMapping("/uploadHead")
    @ResponseBody
    @ApiOperation(value = "图片上传或者修改",notes = "前端要提交的参数名称：file图片，userId是用户id")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "file",value = "图片",required = true),
            @ApiImplicitParam(name = "userId",value = "用户id",required = true)
    })

    @ApiResponses(value = {
            @ApiResponse(code = 200,message = "上传成功"),
            @ApiResponse(code = 500,message = "上传失败")
    })
    public CommonResult uploadHead(MultipartFile file,Integer userId){
        String name= UUID.randomUUID().toString().replace("_","");
        try {
            String url = new QiniuUploadUtil().upload(name, file.getBytes());
            businessImageService.uploadHead(url,userId);
            return CommonResult.success("上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            return CommonResult.failed("上传失败");
        }

    }
}